
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.Common;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.Common
{
    /// <summary>
    /// Flow service
    /// </summary>
    public partial class FlowService : IFlowService
    {
        #region Constants

        /// <summary>
        /// Key for caching
        /// </summary>
        private const string FLOWS_ALL_KEY = "Nop.flow.all";
        /// <summary>
        /// Key for caching
        /// </summary>
        /// <remarks>
        /// {0} : flow ID
        /// </remarks>
        private const string FLOW_BY_ID_KEY = "Nop.flow.id-{0}";
        /// <summary>
        /// Key pattern to clear cache
        /// </summary>
        private const string FLOWS_PATTERN_KEY = "Nop.flow.";
        #endregion
        
        #region Fields
        private readonly IRepository<Flow> _flowRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="flowRepository">Flow repository</param>
        /// <param name="eventPublisher">Event published</param>
        public FlowService(ICacheManager cacheManager,
            IRepository<Flow> flowRepository,
            IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._flowRepository = flowRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets all Flows
        /// </summary>
        /// <returns>Flows</returns>
        public virtual IList<Flow> GetAllFlows()
        {
            string key = FLOWS_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from a in _flowRepository.Table
                            orderby a.Id
                            select a;
                return query.ToList();
            });
        }

        /// <summary>
        /// Gets a Flow 
        /// </summary>
        /// <param name="flowId">Flow identifier</param>
        /// <returns>Flow</returns>
        public virtual Flow GetFlowById(int flowId)
        {
            if (flowId == 0)
                return null;

            string key = string.Format(FLOW_BY_ID_KEY, flowId);
            return _cacheManager.Get(key, () => _flowRepository.GetById(flowId));
        }

        /// <summary>
        /// Inserts a Flow
        /// </summary>
        /// <param name="flow">Flow</param>
        public virtual void InsertFlow(Flow flow)
        {
            if (flow == null)
                throw new ArgumentNullException("flow");

            _flowRepository.Insert(flow);

            _cacheManager.RemoveByPattern(FLOWS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(flow);
        }

        /// <summary>
        /// Updates the Flow
        /// </summary>
        /// <param name="flow">Flow</param>
        public virtual void UpdateFlow(Flow flow)
        {
            if (flow == null)
                throw new ArgumentNullException("flow");

            _flowRepository.Update(flow);

            _cacheManager.RemoveByPattern(FLOWS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(flow);
        }

        /// <summary>
        /// Deletes a Flow
        /// </summary>
        /// <param name="flow">Flow</param>
        public virtual void DeleteFlow(Flow flow)
        {
            if (flow == null)
                throw new ArgumentNullException("flow");

            _flowRepository.Delete(flow);

            _cacheManager.RemoveByPattern(FLOWS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(flow);
        }

        #endregion
    }
}
